import 'package:flutter/material.dart';

class AuthPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AuthPageState();
  }
}

class _AuthPageState extends State<AuthPage> {
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  Map<String, dynamic> _formData = {
    'userName': null,
    'passWord': null,
    'isAgree': false
  };

  Widget _buileNameText() {
    final valid = RegExp(
        r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
    return TextFormField(
      keyboardType: TextInputType.number,
      decoration: InputDecoration(
          labelText: 'Email', fillColor: Colors.white, filled: true),
      validator: (String value) {
        if (!valid.hasMatch(value)) {
          return '用户名不合法';
        }
      },
      onSaved: (String value) {
        setState(() {
          _formData['userName'] = value;
        });
      },
    );
  }

  Widget _buildPasswordText() {
    return TextFormField(
      obscureText: true,
      decoration: InputDecoration(
          labelText: '密码',
          counterText: '8~16位密码',
          filled: true,
          fillColor: Colors.white),
      validator: (String value) {
        if (value.trim().length < 8) {
          return '密码长度必须大于8位';
        }
      },
      onSaved: (String value) {
        setState(() {
          setState(() {
            _formData['passWord'] = value;
          });
        });
      },
    );
  }

  Widget _buildSwith() {
    return SwitchListTile(
      value: _formData['isAgree'],
      onChanged: (bool value) {
        setState(() {
          _formData['isAgree'] = value;
        });
      },
      title: Text('用户协议',
          style: TextStyle(
            color: Colors.black,
          )),
    );
  }

  void _login() {
    // if (!_formKey.currentState.validate()) {
    //   return;
    // }
    _formKey.currentState.save();
    //路由替换(上下文,新路由)
    print(_formData);
    Navigator.pushReplacementNamed(context, '/products');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('登录'),
      ),
      body: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(
                Colors.black.withOpacity(0.5), BlendMode.dstATop),
            image: AssetImage('assets/background.jpg'),
          )),
          margin: EdgeInsets.all(10.0),
          child: Center(
              child: SingleChildScrollView(
                  child: Form(
                      key: _formKey,
                      child: Column(
                        children: <Widget>[
                          _buileNameText(),
                          _buildPasswordText(),
                          _buildSwith(),
                          SizedBox(height: 10.0),
                          RaisedButton(
                            color: Theme.of(context).primaryColor,
                            child: Text('login'),
                            textColor: Colors.white,
                            onPressed: _login,
                          )
                        ],
                      ))))),
    );
  }
}
